14. Solution: For Loops Quiz
Quiz Solution: Create Usernames
names = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
usernames = []
for name in names:
usernames.append(name.lower().replace(" ", "_"))
print(usernames)
Output:
['joey_tribbiani', 'monica_geller', 'chandler_bing', 'phoebe_buffay']
Quiz Solution: Modify Usernames with Range
usernames = ["Joey Tribbiani", "Monica Geller", "Chandler Bing", "Phoebe Buffay"]
for i in range(len(usernames)):
usernames[i] = usernames[i].lower().replace(" ", "_")
print(usernames)
Output:
['joey_tribbiani', 'monica_geller', 'chandler_bing', 'phoebe_buffay']
Quiz Solution: Tag Counter
You can use string indexing to find out if each token begins and ends with angle brackets.
tokens = ['<greeting>', 'Hello World!', '</greeting>']
count = 0
for token in tokens:
if token[0] == '<' and token[-1] == '>':
count += 1
print(count)
Output:
2
Quiz Solution: Create an HTML List
items = ['first string', 'second string']
html_str = "<ul>\n" # The "\n" here is the end-of-line char, causing
# chars after this in html_str to be on next line
for item in items:
html_str += "<li>{}</li>\n".format(item)
html_str += "</ul>"
print(html_str)
Output:
<ul>
<li>first string</li>
<li>second string</li>
</ul>